/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SeanceElement.java
 *
 * Created on 20 janv. 2011, 14:38:04
 */
package org.cinema.view.user;

import java.awt.GridLayout;
import java.sql.Date;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import org.cinema.dao.film.FilmsDao;
import org.cinema.dao.film.FilmsInfo;
import org.cinema.dao.reservation.ReservationsDao;
import org.cinema.dao.reservation.ReservationsInfo;
import org.cinema.dao.salle.SallesDao;
import org.cinema.dao.salle.SallesInfo;
import org.cinema.dao.seance.SeancesInfo;

/**
 *
 * @author med
 */
public final class SeanceElement extends javax.swing.JPanel {

    boolean d3;
    Date date1;
    SallesInfo salles;
    ReservationsInfo reser;
    private SeancesInfo SeancesInfo;

    /** Creates new form SeanceElement */
    public SeanceElement() {

        initComponents();




    }

    public void setSeance(SeancesInfo s) {
        this.SeancesInfo = s;
        d3 = s.isEstTroisD();
        date1 = s.getDebut();
        initReser();
        SallesInfo sale = new SallesDao().getSalle(s.getSalleid());
        FilmsInfo fil = new FilmsDao().getFilm(s.getFilmid());
        date.setText(s.getDebut().toString());
        //salle.setText(sale.getNom());
        //film.setText(fil.getTitre());
    
        nbSeancesDispo.setText(Integer.toString(getNbPlaceDispo(s)));

        salle.setText(sale.getNom());
        film.setText(fil.getTitre());
        heure.setText(s.getHeure().toString());
        if (s.isEstTroisD() == true) {
            TroisD.setText("3D");
        } else {
            TroisD.setText("No");
        }
    }

    public int getNbPlaceDispo(SeancesInfo s) {
        int nbPlaces = 0;
        ReservationsDao reDao = new ReservationsDao();
        SallesDao sDao = new SallesDao();
        /*    List<SallesInfo> l;
        l=sDao.allSalle();
         */
        nbPlaces = sDao.getSalle(s.getSalleid()).getNbPlace();
        /*for (SallesInfo i:l) { if (s.getSalleid()==i.getId()){

        nbPlaces = i.getNbPlace();}
        }*/

//    int IDseance=SeanceElement.getId();
        int palaces = 0;
        List<ReservationsInfo> lr;
        lr = reDao.allReservation();
        for (ReservationsInfo i : lr) // System.out.println(i.getId());
        {
            if (i.getSeanceId()
                    == s.getId()) {
                palaces++;
            }
        }
        // System.out.println(palaces);
        nbPlaces = nbPlaces - palaces;
        return nbPlaces;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        date = new javax.swing.JLabel();
        salle = new javax.swing.JLabel();
        heure = new javax.swing.JLabel();
        film = new javax.swing.JLabel();
        TroisD = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        nbSeancesDispo = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        date.setName("date");// NOI18N

        salle.setName("salle"); // NOI18N

        heure.setName("heure"); // NOI18N

        film.setName("film"); // NOI18N

        TroisD.setName("TroisD"); // NOI18N

        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        nbSeancesDispo.setName("nbSeancesDispo"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(date, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(heure, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(film, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(6, 6, 6).addComponent(salle, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(TroisD, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE).addComponent(nbSeancesDispo, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(28, 28, 28).addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(nbSeancesDispo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE).addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE) //.addComponent(TroisD, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(TroisD, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE).addComponent(salle, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE).addComponent(heure, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE).addComponent(film, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE).addComponent(date, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE)).addContainerGap()));
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed



        {
            JFrame reser = new JFrame();
            
            Reservations r = new Reservations(SeancesInfo, d3, reser);

            reser.getContentPane().add(r);
            reser.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            reser.pack();
            reser.setVisible(true);
            
            r.onNBPlaceChange (this);
        }

    }//GEN-LAST:event_jButton1ActionPerformed
    
   

    public void initReser() {
        Date dateA = new Date(System.currentTimeMillis());
        int nb = getNbPlaceDispo(SeancesInfo);

        if (nb < 1 || (!dateA.toString().equals(date1.toString()))) {
            jButton1.setEnabled(false);
            jButton1.setText("Reservation");
        } else {
            jButton1.setEnabled(true);
        }
        jButton1.setText("Reservation");
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel TroisD;
    private javax.swing.JLabel date;
    private javax.swing.JLabel film;
    private javax.swing.JLabel heure;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel nbSeancesDispo;
    private javax.swing.JLabel salle;
    // End of variables declaration//GEN-END:variables
}
